package com.saicmotor.keeplive;

import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

import com.fanjun.keeplive.KeepLive;
import com.fanjun.keeplive.config.NotificationUtils;
import com.fanjun.keeplive.receiver.NotificationClickReceiver;
import com.fanjun.keeplive.service.HideForegroundService;

/**
 * @Author xinxinzhang
 * @Date 2022/08/19
 **/
public class LocalService extends Service {

    private MediaPlayer mediaPlayer;
    private MyBinder binder;

    @Override
    public void onCreate() {
        super.onCreate();
        if (binder == null) {
            binder = new MyBinder();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 1.播放无声音乐
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.novoice);
            mediaPlayer.setVolume(0f, 0f);
            // 循环
            mediaPlayer.setLooping(true);
            play();
        }
        // 2.启用前台服务，提升优先级
        if (KeepLive.foregroundNotification != null) {
            Intent intent1 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
            intent1.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
            Notification notification = NotificationUtils.createNotification(this,
                    KeepLive.foregroundNotification.getTitle(),
                    KeepLive.foregroundNotification.getDescription(),
                    KeepLive.foregroundNotification.getIconRes(),
                    intent1);
            startForeground(13691, notification);
        }
        // 3.绑定守护进程
        try {
            Intent intent2 = new Intent(this, RemoteService.class);
            bindService(intent2, connection, Context.BIND_ABOVE_CLIENT);
        } catch (Exception ex) {
        }
        // 4.隐藏服务通知
        try {
            if (Build.VERSION.SDK_INT < 25) {
                startService(new Intent(this, HideForegroundService.class));
            }
        } catch (Exception ex) {
        }
        if (KeepLive.keepLiveService != null) {
            KeepLive.keepLiveService.onWorking();
        }
        return Service.START_STICKY;
    }

    // 播放
    private void play() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

    private class MyBinder extends IMyAidlInterface.Stub {


        @Override
        public void wakeUp(String title, String discription, int iconRes) throws RemoteException {

        }
    }

    private ServiceConnection connection = new MyServiceConnection(this);

    private class MyServiceConnection implements ServiceConnection {

        LocalService service;

        MyServiceConnection(LocalService service) {
            this.service = service;
        }

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            // TODO:服务绑定成功时回调
            if (binder != null && KeepLive.foregroundNotification != null) {
                IMyAidlInterface myAidl = IMyAidlInterface.Stub.asInterface(iBinder);
                // ...
                try {
                    myAidl.wakeUp(KeepLive.foregroundNotification.getTitle(),
                    KeepLive.foregroundNotification.getDescription(),
                    KeepLive.foregroundNotification.getIconRes());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Intent remote = new Intent(service, RemoteService.class);
            service.startService(remote);
            Intent intent = new Intent(service, RemoteService.class);
            service.bindService(intent, this, Context.BIND_ABOVE_CLIENT);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        if (KeepLive.keepLiveService != null) {
            KeepLive.keepLiveService.onStop();
        }
    }
}
